package com.geh.learn.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CalcTest002 {

    public static Integer calc001(Integer para) {
        try {
            TimeUnit.SECONDS.sleep(12);
        } catch (Exception e) {
        }
        return para * para;
    }

    public static Integer calc002(Integer para) {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e) {
        }
        return para * para;
    }

    public static Integer done() {
        System.out.println("done");
        return 0;
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        final CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> calc001(50));
        final CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> calc002(50));

        // final CompletableFuture<Integer> done = CompletableFuture.supplyAsync(() -> done());
        long start = System.currentTimeMillis();
        CompletableFuture doneF = CompletableFuture.anyOf(f1, f2);
        doneF.get();
        System.out.println(System.currentTimeMillis() - start);



    }

}
